package com.anolesoft.amljmes.delivery.controller;

import java.util.ArrayList;
import java.util.List;

import com.anolesoft.amljmes.delivery.deliwebservice.dto.MesBzzScjljhDto;
import com.anolesoft.amljmes.delivery.deliwebservice.dto.WebDto;
import com.anolesoft.amljmes.delivery.deliwebservice.mapper.DeliWebMapper;
import com.anolesoft.amljmes.delivery.deliwebservice.service.DeliWebService;
import com.anolesoft.amljmes.delivery.mapper.MesScjljhMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import com.anolesoft.yr.util.HttpStatus;

import javax.annotation.Resource;

/**
 * 
 * @author 宋佳明
 * @date 2022/6/28
 *
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/scjljh")
public class MesScjljhController {

	@Autowired
	private DeliWebService webService;
	@Resource
	private DeliWebMapper mapper;
	@Resource
	private MesScjljhMapper scjljhMapper;

	/**
	 * 从全景物流获取数据
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/querylist", method = RequestMethod.POST)
	public AjaxResult queryScjljh(@RequestBody MesBzzScjljhDto dto) {

		List<MesBzzScjljhDto> allList = null;
		try {
			dto.setModel(dto.getModel().trim());
			dto.setPartDrawingNo(dto.getPartDrawingNo().trim());
			dto.setPgh(dto.getPgh().trim());
			allList = webService.getArrList(dto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Integer pageNum = dto.getPageNum();
		Integer pageSize = dto.getPageSize();
		int endIndex = pageNum * pageSize > allList.size() ? allList.size() : pageNum * pageSize;
		List<MesBzzScjljhDto> list = allList.subList( (pageNum-1) *pageSize, endIndex);
		PageResult pageResult = new PageResult(list, pageSize, pageNum, allList.size());
		return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageResult);

	}

	/**
	 * 修改全景物流库存子表计划类型,插入封存防护计划
	 * @param dto
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "/updateScjljh", method = RequestMethod.POST)
	public AjaxResult updateScjljh(@RequestBody WebDto dto) {
		LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
		//当前登录人主键
		String objuid = loginUser.getEmpUid();
		String fcode = loginUser.getFacCode();
		int a = 0;
		List<String> objuids = dto.getObjuids();
		List<MesBzzScjljhDto> mesBzzScjljhDtos = dto.getMesBzzScjljhDtos();
		try {
//			a = webService.updateIns(objuids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<String> list = new ArrayList<>();
		for (MesBzzScjljhDto items:
			 mesBzzScjljhDtos) {
			int i = scjljhMapper.countfcfh(items.getQualifiedLabelNo());
			if (i == 0) {
				items.setCuser(objuid);
				items.setFcode(fcode);
				a = mapper.insertFcfhjh(items);
			} else {
				list.add(items.getQualifiedLabelNo());
			}
		}
		return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,list);

	}

}
